package com.supervision.manage.controller;

import net.coobird.thumbnailator.Thumbnails;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

@RestController
public class ToolController {

    public static void main(String[] args) throws IOException {
        String txt = "北京时间2020年3月31日14:54";
        int width = 220;
        int height = 20;
        //创建内存图像
        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 获取图形上下文
        Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics();

        //画背景图
        graphics.setColor(new Color(0, 0, 0));
        graphics.fillRect(0, 0, width, height);

        //字体大小为图片高度的80%
        int fsize = (int) (height * 0.8);
        int fx = height - fsize;
        int fy = fsize;
        //设定字体
        Font font = new Font("微软雅黑", Font.PLAIN, 14);
        graphics.setFont(font);
        graphics.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);//设置抗锯齿
        graphics.setPaint(new Color(0, 0, 0, 64));//阴影颜色
        graphics.drawString(txt, fx, fy);//先绘制阴影
        graphics.setPaint(Color.WHITE);//正文颜色
        graphics.drawString(txt, fx, fy);//用正文颜色覆盖上去

        graphics.dispose();

        ImageIO.write(bufferedImage, "jpg", new File("/Users/whis/Downloads/a.jpg"));

//        BufferedImage img = Thumbnails.of(bufferedImage).scale(1f).outputQuality(1f).asBufferedImage();
//        ImageIO.write(img, "jpg", new File("/Users/whis/Downloads/a.jpg"));

    }

}
